
package com.bluelinelabs.conductor.demo.controllers;

import com.bluelinelabs.conductor.conductor.manager.ObjectManager;
import ohos.aafwk.content.IntentParams;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import com.bluelinelabs.conductor.conductor.ControllerChangeHandler;
import com.bluelinelabs.conductor.conductor.ControllerChangeType;
import com.bluelinelabs.conductor.conductor.RouterTransaction;
import com.bluelinelabs.conductor.conductor.changehandler.HorizontalChangeHandler;
import com.bluelinelabs.conductor.demo.ResourceTable;
import com.bluelinelabs.conductor.demo.controllers.base.BaseController;
import com.bluelinelabs.conductor.demo.util.ColorUtil;


/**
 * lipeiquan
 *
 * @since 2021-05-06
 */
public class NavigationDemoController extends BaseController {
    public static final String TAG_UP_TRANSACTION = "NavigationDemoController.up";
    public static final String KEY_INDEX = "NavigationDemoController.index";
    public static final String KEY_DISPLAY_UP_MODE = "NavigationDemoController.displayUpMode";
    private int index;
    private DisplayUpMode displayUpMode;
    private boolean mFlag = true;


    /**
     * Constructor that takes arguments that need to be retained across restarts.
     *
     * @param args Any arguments that need to be retained.
     */
    public NavigationDemoController(IntentParams args) {
        super(ResourceTable.Layout_controller_navigation_demo, args);
        index = (int) args.getParam(KEY_INDEX);
        displayUpMode = DisplayUpMode.values()[(int) args.getParam(KEY_DISPLAY_UP_MODE)];
    }

    public NavigationDemoController(int index, DisplayUpMode displayUpMode) {
        super(ResourceTable.Layout_controller_navigation_demo);
        this.index = index;
        this.displayUpMode = DisplayUpMode.values()[(int) displayUpMode.ordinal()];
    }

    public NavigationDemoController(int index, DisplayUpMode displayUpMode, boolean flag) {
        super(ResourceTable.Layout_controller_navigation_demo);
        this.index = index;
        this.displayUpMode = DisplayUpMode.values()[(int) displayUpMode.ordinal()];
        mFlag = flag;
    }


    @Override
    public void onViewCreated(Component component) {

        if (mFlag) {
            title = "Navigation Demos";
        }
        ObjectManager.getInstance().setIdTitleAboutSetVisibility(Component.HIDE);
        Component popToRoot = component.findComponentById(ResourceTable.Id_popToRoot);
        Component goUp = component.findComponentById(ResourceTable.Id_goUp);
        Component goToNext = component.findComponentById(ResourceTable.Id_goToNext);
        Text title = (Text) component.findComponentById(ResourceTable.Id_title);

        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor(ColorUtil.getMaterialColor(component.getContext(), index))));
        component.setBackground(shapeElement);

        title.setText(component.getContext().getString(ResourceTable.String_navigation_title, index));
        goUp.setVisibility(displayUpMode == DisplayUpMode.SHOW ? Component.VISIBLE : Component.HIDE);


        popToRoot.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getRouter().popToRoot();
            }
        });

        goUp.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getRouter().popToTag(TAG_UP_TRANSACTION);
            }
        });

        goToNext.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                IntentParams intentParams = new IntentParams();
                intentParams.setParam(NavigationDemoController.KEY_INDEX, index + 1);
                intentParams.setParam(NavigationDemoController.KEY_DISPLAY_UP_MODE, displayUpMode.displayUpModeForChild().ordinal());
                getRouter().pushController(RouterTransaction.with(new NavigationDemoController(index + 1, displayUpMode.displayUpModeForChild(),false))
                    .pushChangeHandler(new HorizontalChangeHandler())
                    .popChangeHandler(new HorizontalChangeHandler())
                );
            }
        });


    }

    public enum DisplayUpMode {
        SHOW, SHOW_FOR_CHILDREN_ONLY, HIDE;

        public DisplayUpMode displayUpModeForChild() {
            if (this == HIDE) {
                return HIDE;
            } else {
                return SHOW;
            }
        }
    }


    @Override
    protected void onChangeEnded(ControllerChangeHandler changeHandler, ControllerChangeType changeType) {
        super.onChangeEnded(changeHandler, changeType);
        if (changeType.isEnter) {
            setButtonsEnabled(true);
        }
    }


    @Override
    protected void onChangeStarted(ControllerChangeHandler changeHandler, ControllerChangeType changeType) {
        super.onChangeStarted(changeHandler, changeType);
        setButtonsEnabled(false);
    }


    private void setButtonsEnabled(Boolean enabled) {
        getView().findComponentById(ResourceTable.Id_popToRoot).setEnabled(enabled);
        getView().findComponentById(ResourceTable.Id_goUp).setEnabled(enabled);
        getView().findComponentById(ResourceTable.Id_goToNext).setEnabled(enabled);
        setButtonsTextColor(enabled);
        setButtonsTextColor(enabled);
    }

    private void setButtonsTextColor(Boolean enabled) {
        Color color;
        if (enabled) {
            color = new Color(Color.getIntColor(getActivity().getString(ResourceTable.Color_colorAccent)));
        } else {
            color = new Color(Color.getIntColor("#D0D2D1"));
        }
        ((Button) getView().findComponentById(ResourceTable.Id_popToRoot)).setTextColor(color);
        ((Button) getView().findComponentById(ResourceTable.Id_goUp)).setTextColor(color);
        ((Button) getView().findComponentById(ResourceTable.Id_goToNext)).setTextColor(color);
    }

}
